//
//  HBDiscoverItemController.m
//  HeartBeat
//
//  Created by karong on 2023/8/23.
//

#import "HBDiscoverItemController.h"
#import "HBDiscoverItemRequest.h"
#import "HBChannelModel.h"
#import "HBChannelItemModel.h"
#import "HBDiscoverItemBannerCell.h"
#import "HBDiscoverItemExtraCell.h"
#import "HBDiscoverItemNovel1Cell.h"
#import "HBDiscoverItemNovel2Cell.h"
#import "HBDiscoverItemRankListCell.h"
#import "HBDiscoverItemHorizontalCell.h"

@interface HBDiscoverItemController () <HBDiscoverItemBannerCellDelegate, HBDiscoverItemRankListCellDelegate, HBDiscoverItemHorizontalCellDelegate, HBDiscoverItemExtraCellDelegate>
@property (weak, nonatomic) IBOutlet UICollectionView *listView;
@property (nonatomic, strong) NSArray * channelArray;
@property (nonatomic, strong) HBDiscoverItemRequest * request;
@end

@implementation HBDiscoverItemController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.listView reloadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = UIColor.bg_gray_f2f2f2;
    HBDiscoverItemRequest * request = [HBDiscoverItemRequest new];
    request.channelId = self.channel.id;
    request.channelType = self.channel.channel_type;
    self.request = request;
    UINib * nib1 = [UINib nibWithNibName:@"HBDiscoverItemNovel1Cell" bundle:nil];
    [self.listView registerNib:nib1 forCellWithReuseIdentifier:@"HBDiscoverItemNovel1Cell"];
    UINib * nib2 = [UINib nibWithNibName:@"HBDiscoverItemNovel2Cell" bundle:nil];
    [self.listView registerNib:nib2 forCellWithReuseIdentifier:@"HBDiscoverItemNovel2Cell"];
    self.view.backgroundColor = UIColor.clearColor;
}

- (void)showNovelDetailWithNovel:(id)novel {
    [ViewManager pushNovelDetailVCWithNovel:novel inController:self];
}

#pragma mark RefreshResultProtocol
- (void)loadSuccessWithArray:(NSArray *)array isRefresh:(BOOL)isRefresh {
    self.channelArray = self.request.channelArray;
    [super loadSuccessWithArray:array isRefresh:isRefresh];
}

- (BOOL)disableAutoBeginRefresh {
    return YES;
}

#pragma mark UICollectionDelegte
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return self.request.channelArray.count + MIN(self.dataArray.count, 1);
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    NSInteger count = 0;
    if (section < self.channelArray.count) {
        HBChannelItemModel * channelItemModel = self.channelArray[section];
        count = channelItemModel.row;
    }
    else {
        count = self.dataArray.count;
    }
    return count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    HBBaseCollectionViewCell * cell;
    if (indexPath.section < self.channelArray.count) {
        HBChannelItemModel * channelItemModel = self.channelArray[indexPath.section];
        NSString * cellClass = channelItemModel.cellClass;
        if ([cellClass isEqualToString:@"HBDiscoverItemNovel1Cell"]) {
            HBDiscoverItemNovel1Cell * discoverItemNovel1Cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HBDiscoverItemNovel1Cell" forIndexPath:indexPath];
            [discoverItemNovel1Cell setDataWithObject:channelItemModel.items[indexPath.row]];
            cell = discoverItemNovel1Cell;
        }
        else if ([cellClass isEqualToString:@"HBDiscoverItemBannerCell"]) {
            HBDiscoverItemBannerCell * bannerCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HBDiscoverItemBannerCell" forIndexPath:indexPath];
            bannerCell.delegate = self;
            [bannerCell setDataWithObject:channelItemModel.items];
            cell = bannerCell;
        }
        else if ([cellClass isEqualToString:@"HBDiscoverItemExtraCell"]) {
            HBDiscoverItemExtraCell * discoverItemExtraCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HBDiscoverItemExtraCell" forIndexPath:indexPath];
            discoverItemExtraCell.delegate = self;
            [discoverItemExtraCell setDataWithObject:channelItemModel.items];
            cell = discoverItemExtraCell;
        }
        else if ([cellClass isEqualToString:@"HBDiscoverItemRankListCell"]) {
            HBDiscoverItemRankListCell * discoverItemRankListCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HBDiscoverItemRankListCell" forIndexPath:indexPath];
            discoverItemRankListCell.delegate = self;
            [discoverItemRankListCell setDataWithObject:channelItemModel];
            cell = discoverItemRankListCell;
        }
        else if ([cellClass isEqualToString:@"HBDiscoverItemHorizontalCell"]) {
            HBDiscoverItemHorizontalCell * discoverItemHorizontalCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HBDiscoverItemHorizontalCell" forIndexPath:indexPath];
            discoverItemHorizontalCell.delegate = self;
            [discoverItemHorizontalCell setDataWithObject:channelItemModel];
            cell = discoverItemHorizontalCell;
        }
        else {
            HBDiscoverItemNovel2Cell * discoverItemNovel2Cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HBDiscoverItemNovel2Cell" forIndexPath:indexPath];
            [discoverItemNovel2Cell setDataWithObject:channelItemModel.items[indexPath.row]];
            cell = discoverItemNovel2Cell;
        }
    }
    else {
        HBDiscoverItemNovel1Cell * discoverItemNovel1Cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HBDiscoverItemNovel1Cell" forIndexPath:indexPath];
        [discoverItemNovel1Cell setDataWithObject:self.dataArray[indexPath.row]];
        cell = discoverItemNovel1Cell;
    }
    return cell;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        UICollectionReusableView * discoverItemHeaderView;
        if (!discoverItemHeaderView) {
            discoverItemHeaderView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"HBDiscoverItemHeaderView" forIndexPath:indexPath];
        }
        return discoverItemHeaderView;
    }
    return nil;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    CGSize size = CGSizeZero;
    if (indexPath.section < self.channelArray.count) {
        HBChannelItemModel * channelItemModel = self.channelArray[indexPath.section];
        size = channelItemModel.cellSize;
    }
    else {
        CGFloat width = floor((HBScreenWidth - 12 - 12 - 12) / 2.0f);
        CGFloat height = width * 245 / 162.0f;
        size.width = width;
        size.height = height;
    }
    return size;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    CGSize size = CGSizeZero;
    size.width = HBScreenWidth;
    if (section >= self.channelArray.count) {
        size.height = 55;
    }
    return size;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    UIEdgeInsets inset = UIEdgeInsetsZero;
    if (section < self.channelArray.count) {
        HBChannelItemModel * channelItemModel = self.channelArray[section];
        inset = channelItemModel.inset;
    }
    else {
        inset.left = 12;
        inset.right = 12;
    }
    return inset;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section < self.channelArray.count) {
        HBChannelItemModel * channelItemModel = self.channelArray[indexPath.section];
        id object = channelItemModel.items[indexPath.row];
        if ([object isKindOfClass:NSClassFromString(@"HBNovelModel")]) {
            [self showNovelDetailWithNovel:object];
        }
    }
    else {
        id object = self.dataArray[indexPath.row];
        if ([object isKindOfClass:NSClassFromString(@"HBNovelModel")]) {
            [self showNovelDetailWithNovel:object];
        }
    }
    
    
    
}
#pragma mark HBDiscoverItemBannerCellDelegate
- (void)discoverItemBannerCellAction:(HBBaseCollectionViewCell *)cell object:(HBItemModel *)object {
    [ViewManager viewControllJumpWithjumpType:object.jump_type vc:self dataDic:@{@"book_id":@(object.book_id),@"jump_url":KEmptyStr(object.jump_url)}];
}

#pragma mark HBDiscoverItemNovelListCellDelegate
- (void)discoverItemNovelListCellDidSelectWithObject:(id)object {
    [self showNovelDetailWithNovel:object];
}

#pragma mark HBDiscoverItemRankListCellDelegate
- (void)discoverItemRankListCellDidSelectWithObject:(id)object {
    [self showNovelDetailWithNovel:object];
}

#pragma mark HBDiscoverItemHorizontalCellDelegate
- (void)discoverItemHorizontalCellDidSelectWithObject:(id)object {
    [self showNovelDetailWithNovel:object];
}

#pragma mark HBDiscoverItemExtraCellDelegate
- (void)discoverItemExtraCellBtnAction:(HBItemModel *)object {
    [ViewManager viewControllJumpWithjumpType:object.jump_type vc:self dataDic:@{@"book_id":@(object.book_id),@"jump_url":KEmptyStr(object.jump_url)}];
}
@end
